package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SeriesInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ProgramInfoMapper;
import com.ruoyi.system.domain.ProgramInfo;
import com.ruoyi.system.service.IProgramInfoService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 节目信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-03-19
 */
@Service
@Transactional
public class ProgramInfoServiceImpl implements IProgramInfoService
{
    @Autowired
    private ProgramInfoMapper programInfoMapper;

    /**
     * 查询节目信息
     *
     * @param id 节目信息ID
     * @return 节目信息
     */
    @Override
    public ProgramInfo selectProgramInfoById(String id)
    {
        return programInfoMapper.selectProgramInfoById(id);
    }

    /**
     * 查询节目信息列表
     *
     * @param programInfo 节目信息
     * @return 节目信息
     */
    @Override
    public List<ProgramInfo> selectProgramInfoList(ProgramInfo programInfo)
    {
        return programInfoMapper.selectProgramInfoList(programInfo);
    }

    /**
     * 新增节目信息
     *
     * @param programInfo 节目信息
     * @return 结果
     */
    @Override
    public int insertProgramInfo(ProgramInfo programInfo)
    {
        return programInfoMapper.insertProgramInfo(programInfo);
    }

    /**
     * 修改节目信息
     *
     * @param programInfo 节目信息
     * @return 结果
     */
    @Override
    public int updateProgramInfo(ProgramInfo programInfo)
    {
        return programInfoMapper.updateProgramInfo(programInfo);
    }

    /**
     * 删除节目信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteProgramInfoByIds(String ids)
    {
        return programInfoMapper.deleteProgramInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除节目信息信息
     *
     * @param id 节目信息ID
     * @return 结果
     */
    @Override
    public int deleteProgramInfoById(String id)
    {
        return programInfoMapper.deleteProgramInfoById(id);
    }

    /**
     * 数据导入
     * @param programInfoList
     * @return
     */
    @Override
    public String importInfo(List<ProgramInfo> programInfoList, String userName) {
        if (StringUtils.isNull(programInfoList) || programInfoList.size() == 0) {
            throw new BusinessException("导入节目数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ProgramInfo programInfo : programInfoList) {
            try {
                programInfo.setCreateBy(userName);
                programInfoMapper.insertProgramInfo(programInfo);
                successNum++;
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + "第" + failureNum + "条导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }
        return successMsg.toString();
    }
}
